package com.luobd.bpm.controller;

import cn.hutool.core.util.StrUtil;
import com.luobd.bpm.common.PageInput;
import com.luobd.bpm.dto.ProcessTaskDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

//@RestController
//@RequestMapping(value = "/api/bpmn/task")
//@Api(tags = "流程任务管理")
public class BpmnTaskController {


    @Autowired
    private TaskService taskService;



    @PostMapping(value = "/page")
    @ApiOperation(value = "分页查询流程任务")
    public List<ProcessTaskDTO> getTaskPage(@RequestBody ProcessTaskPageInput input) {
        TaskQuery taskQuery = taskService.createTaskQuery();
        if(StrUtil.isNotBlank(input.getAssignee())) {
            taskQuery.taskAssignee(input.getAssignee());
        }
        List<Task> list = taskQuery.orderByTaskCreateTime().desc().listPage(input.getStart(), input.getPageSize());
        return list.stream().map(ProcessTaskDTO::new).collect(Collectors.toList());
    }

    @PostMapping(value = "/setAssignee")
    @ApiOperation(value = "设置流程任务办理人")
    public String setAssignee(@RequestBody ProcessTaskSetAssigneeInput input) {
        taskService.setAssignee(input.getTaskId(), input.getAssignee());
        return "ok";
    }



    @PostMapping(value = "/complete")
    @ApiOperation(value = "完成任务")
    public String complete(@RequestBody ProcessCompleteInput input) {
        taskService.complete(input.getTaskId());
        return "ok";
    }




}

@EqualsAndHashCode(callSuper = true)
@Data
@ApiModel(description = "流程任务分页查询")
class ProcessTaskPageInput extends PageInput {
    @ApiModelProperty(value = "任务办理人")
    private String assignee;
}

@Data
@ApiModel(description = "设置流程任务办理人")
class ProcessTaskSetAssigneeInput {
    @ApiModelProperty(value = "任务id",required = true)
    private String taskId;
    @ApiModelProperty(value = "任务办理人",required = true)
    private String assignee;
}

@Data
@ApiModel(description = "完成任务")
class ProcessCompleteInput {

    @ApiModelProperty(value = "任务id",required = true)
    private String taskId;
}


